<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {


	function __construct()
    {     	
    	parent::__construct();
		$this->load->model('category'); 
		$this->load->model('user'); 
		$this->load->model('adeal'); 
		$this->load->helper('coupon');
		
    }

    public function test() {
    	createCoupon(1,'Agsdfgsdfg');
    }

    
    public function index(){	
    	$data["categories"] = $this->category->getPageCategories();
		$data["session_id"] = $this->session->userdata('id');
		$data["session_name"] = $this->session->userdata('name');
		$data["adeals"] = $this->adeal->getFirstDeals();
		$this->load->view('home.php', $data);
	}

	public function all() {
		$data["categories"] = $this->category->getPageCategories();
		$data["session_id"] = $this->session->userdata('id');
		$data["session_name"] = $this->session->userdata('name');

		$data["page"] = 1;
		for ($i = 0; $i < func_num_args(); $i+=2) {
			$data[func_get_arg($i)] = func_get_arg($i + 1);
		}
		if(!isset($data["category"])) {
			$data["adeals"] = $this->adeal->getPageDeals($data["page"]);
		} else {
			$data["adeals"] = $this->adeal->getFilteredRows(array('category'=>$data['category']));
		}
		$this->load->view('all.php', $data);
	}

	public function signin() {
		
		if ($this->session->userdata('id')) {
			header('Location: /wow/home/');
		} else {
			if ((func_num_args()>0) && (func_get_arg(0) == "error")) {
				$data["error"] = "Неверный email или пароль";
				$this->load->view('signin.php',$data);
			} else $this->load->view('signin.php');
			
		}
	}

	public function cabinet() {
		$data["session_name"] = $this->session->userdata('name');
		$data["avatar"] = $this->user->getUserAvatar($this->session->userdata('id'));
		if (!$this->session->userdata('id')) {
			header('Location: /wow/home/');
		}
		$this->load->view('cabinet.php', $data);
		
	}

	public function login() {
		$this->load->model('user');
		if ($user = $this->user->userExist($_POST['email'],md5($_POST['pass']))) {
			$this->session->set_userdata('id', $user[0]->ID);
			$this->session->set_userdata('name', $user[0]->name);
			header('Location: /wow/home/');
		} else {
			header('Location: /wow/home/index/error_login');
		}
	}

	public function signup(){
		if ((func_num_args()>0) && (func_get_arg(0) == "error")) {
			$data["error"] = "Ошибка";
			$this->load->view('signup.php',$data);
		} else $this->load->view('signup.php');
	}

    function register() {
        $pass = md5($_POST['pass']);
        $this->user->addUser($_POST['name'], 
            $_POST['email']
            ,$pass,$_POST['city']);
        header('Location: /wow');
    }

    public function logout() {
		$this->session->sess_destroy();
		header('Location: /wow');
	}

	function feedback() {
		$data["categories"] = $this->category->getPageCategories();
		$data["session_id"] = $this->session->userdata('id');
		$data["session_name"] = $this->session->userdata('name');
		$data["adeals"] = $this->adeal->getFirstDeals();

		$this->load->view('feedback.php', $data);
	}

	public function loadAva(){
		$config['upload_path'] = './images/';
		$config['allowed_types'] = 'jpg|jpeg|png|gif';
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload()) {
			$error = array('error' => $this->upload->display_errors());
			$this->load->view('upload_error', $error);
		} else {
			$data = $this->upload->data();
			$img_url = "/images/".$data["file_name"];

			$config['image_library'] = 'gd2';
			$config['source_image']	= '.'.$img_url;
			$config['new_image']	= './images/userava/'.$data["file_name"];
			$config['maintain_ratio'] = FALSE;
			$config['width']	 = 150;
			$config['height']	= 150;
			$this->load->library('image_lib', $config); 
			$this->image_lib->resize();

			unlink('.'.$img_url);

			$data = array(
    			'avatar' => '/images/userava/'.$data["file_name"]
    		 );
			$this->db->where('ID', $this->session->userdata('id'));
			$this->db->set($data);
    		$this->db->update('users');
    		header('Location: /wow/home/cabinet/');
		}
	}

	public function orderqiwiBill() {
		$user_phone = 7014301858;
		$service_id = 32;//Service id (coupon/discount)
		
		$price = 1234.56;//Price of service (normally must be found by $service_id)

		$txn = 'FF';//Id of order in Orders table (generated when new order with such user and service created)
		$comment = 'Something great!!!';

		$x = '<?xml version="1.0" encoding="utf-8"?><request>';
		$x .= '<protocol-version>4.00</protocol-version>';
		$x .= '<request-type>30</request-type>';
		$x .= '<extra name="password">1010101010</extra>';
		$x .= '<terminal-id>657148</terminal-id>';
		$x .= '<extra name="txn-id">' . $txn. '</extra>';
		$x .= '<extra name="to-account">' . $user_phone . '</extra>';
		$x .= '<extra name="amount">' . $price . '</extra>';
		$x .= '<extra name="comment">' . $comment . '</extra>';
		$x .= '<extra name="create-agt">' . 0 . '</extra>';
		$x .= '<extra name="ltime">' . 1800 . '</extra>';
		$x .= '<extra name="ALARM_SMS">' . 0 . '</extra>';
		$x .= '<extra name="ACCEPT_CALL">' . 0 . '</extra>';
		$x .= '</request>';


		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'http://www.mobw.ru/term2/xmlutf.jsp');
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type' => 'text/xml; encoding=utf-8'));
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $x);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$response = curl_exec($ch);
		curl_close($ch);
		echo $response;
	}

	public function checkqiwiBills() {
		$x = '<?xml version="1.0" encoding="utf-8"?><request>';
		$x .= '<protocol-version>4.00</protocol-version>';
		$x .= '<request-type>30</request-type>';
		$x .= '<extra name="password">1010101010</extra>';
		$x .= '<terminal-id>657148</terminal-id>';
		$x .= '<bills-list>';
		$x .= '<bill txn-id="FF00"/>';
		$x .= '<bill txn-id="FF01"/>';
		$x .= '</bills-list>';
		$x .= '</request>';


		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'http://www.mobw.ru/term2/xmlutf.jsp');
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type' => 'text/xml; encoding=utf-8'));
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $x);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$response = curl_exec($ch);
		curl_close($ch);
		echo $response;
	}
}